package com.tianzeculture.stagebao.activity.home.popwindow;

import android.app.Activity;
import android.content.Context;
import android.graphics.Rect;
import android.graphics.drawable.ColorDrawable;
import android.os.Build;
import android.view.Display;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;

import com.tianzeculture.stagebao.R;
import com.tianzeculture.stagebao.activity.home.adapter.WorkTagAdapter;
import com.tianzeculture.stagebao.view.MyGridView;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2018/6/5 0005.
 * 人才类型
 */

public class TalentTypePopWindow extends PopupWindow {
    private Activity mContext;
    private final LayoutInflater inflater;
    private View searchView;
    private final Display display;
    private final WindowManager windowManager;
    private TalentTypeListener talentTypeListener;

    MyGridView gvPaishe, gvHuodong, gvBiaoyan;

    public TalentTypePopWindow(Activity context) {
        this.mContext = context;
        inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        searchView = inflater.inflate(R.layout.popwindow_talent_type, null);

        this.setContentView(searchView);
        //设置CityPickerPopWindow弹出窗体的宽
        windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        display = windowManager.getDefaultDisplay();
        this.setWidth(RelativeLayout.LayoutParams.FILL_PARENT);
        //设置CityPickerPopWindow弹出窗体的高
//        this.setHeight((int) (display.getHeight() * 0.35));
        this.setHeight((int) (RelativeLayout.LayoutParams.WRAP_CONTENT));
        //设置CityPickerPopWindow弹出窗体可点击
        this.setFocusable(true);
        //设置CityPickerPopWindow可触摸
        this.setTouchable(true);
        //设置CityPickerPopWindow弹出窗体动画效果
//        this.setAnimationStyle(R.style.AnimBottom);
        //实例化一个ColorDrawable颜色为半透明
        ColorDrawable dw = new ColorDrawable(0x9000000);
        //设置CityPickerPopWindow弹出窗体的背景
        this.setBackgroundDrawable(dw);
        this.setOutsideTouchable(true);
        setUpListener(searchView);
    }

    List<String> paisheTags = new ArrayList<>();//平面拍摄
    List<String> huodongTags = new ArrayList<>();//会展活动
    List<String> biaoyanTags = new ArrayList<>();//影视表演

    private void initTag() {
        paisheTags.clear();
        paisheTags.add("时尚人像");
        paisheTags.add("时装拍摄");
        paisheTags.add("婚纱样片");
        paisheTags.add("汽车广告");
        paisheTags.add("泳装拍摄");
        paisheTags.add("内衣拍摄");
        paisheTags.add("个护化妆");
        paisheTags.add("珠宝配饰");
        paisheTags.add("游戏动漫");
        paisheTags.add("数码产品");
        paisheTags.add("家具用品");
        paisheTags.add("其他用品");

        huodongTags.clear();
        huodongTags.add("车展");
        huodongTags.add("T台走秀");
        huodongTags.add("发型秀");
        huodongTags.add("动漫游戏");
        huodongTags.add("商场演出");
        huodongTags.add("其他活动");
        huodongTags.add("礼仪模特");

        biaoyanTags.clear();
        biaoyanTags.add("视频广告");
        biaoyanTags.add("MV拍摄");
        biaoyanTags.add("电影拍摄");
        biaoyanTags.add("微电影拍摄");
        biaoyanTags.add("娱乐节目");
        biaoyanTags.add("电视剧");
        biaoyanTags.add("其他演出");
    }

    public void setUpListener(View searchView) {
        initTag();
        gvPaishe = searchView.findViewById(R.id.gvPaishe);
        gvHuodong = searchView.findViewById(R.id.gvHuodong);
        gvBiaoyan = searchView.findViewById(R.id.gvBiaoyan);
        WorkTagAdapter paisheTagAdapter = new WorkTagAdapter(mContext, paisheTags);
        gvPaishe.setAdapter(paisheTagAdapter);
        WorkTagAdapter huodongTagAdapter = new WorkTagAdapter(mContext, huodongTags);
        gvHuodong.setAdapter(huodongTagAdapter);
        WorkTagAdapter biaoyanTagAdapter = new WorkTagAdapter(mContext, biaoyanTags);
        gvBiaoyan.setAdapter(biaoyanTagAdapter);

        Button btnSure = searchView.findViewById(R.id.btnSure);
        btnSure.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                talentTypeListener.pickTalentTypeValue("sadad");
            }
        });
    }


    /**
     * 显示popupWindow
     *
     * @param parent
     */
    public void showPopupWindow(View parent) {
        if (!this.isShowing()) {
            // 以下拉方式显示popupwindow
//            this.showAtLocation(parent, Gravity.CENTER_HORIZONTAL, 0, 0); //设置layout在PopupWindow中显示的位置
            this.showAsDropDown(parent);
        } else {
            this.dismiss();
        }
    }


    public interface TalentTypeListener {
        void pickTalentTypeValue(String value);

        void talentTypeCancel();
    }

    public void setTalentTypeListener(TalentTypeListener listener) {
        this.talentTypeListener = listener;
    }

    @Override
    public void showAsDropDown(View anchor) {
        if (Build.VERSION.SDK_INT >= 24) {
            Rect rect = new Rect();
            anchor.getGlobalVisibleRect(rect);
            int h = anchor.getResources().getDisplayMetrics().heightPixels - rect.bottom;
            setHeight(h);
        }
        super.showAsDropDown(anchor);
    }

    @Override
    public void dismiss() {
        super.dismiss();
        talentTypeListener.talentTypeCancel();
    }
}
